﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using EFMysql;
using System.Linq.Expressions;
using CustomMVC;
using System.Diagnostics;

namespace BLL
{
    public class OrderBll:BaseBll
    {
        public List<pre_order> GetOrderList(pre_orderSearch search, int pageSize, int pageNum, ref int totalRecord)
        {
            List<Conditioner> condition = new List<Conditioner>();
            if (search.orderid.HasValue)
            {
                condition.Add(new Conditioner { Key = "orderid", Value = search.orderid.ToString(), Contract = "=" });
            }
            if (!string.IsNullOrEmpty(search.txtOrderNo))
            {
                condition.Add(new Conditioner { Key = "orderno", Value = search.txtOrderNo.ToString(), Contract = "=" });
            }
            if (search.txtOrderYuyueStart.HasValue)
            {
                condition.Add(new Conditioner { Key = "yuyuetime", Value = search.txtOrderYuyueStart.ToString(), Contract = ">" });
            }
            if (search.txtOrderYuyueEnd.HasValue)
            {
                condition.Add(new Conditioner { Key = "yuyuetime", Value = search.txtOrderYuyueEnd.ToString(), Contract = "<" });
            }
            if (search.txtBuyTimeStart.HasValue)
            {
                condition.Add(new Conditioner { Key = "buytime", Value = search.txtBuyTimeStart.ToString(), Contract = ">" });
            }
            if (search.txtBuyTimeEnd.HasValue)
            {
                condition.Add(new Conditioner { Key = "buytime", Value = search.txtBuyTimeEnd.ToString(), Contract = "<" });
            }
            List<pre_order> orderList = WebVar<pre_order>.GetList;

           
            if (orderList.Count > 0)
            {
                Expression<Func<pre_order, bool>> exp = LinqHelperExtension.GetExp(new pre_order(), condition.ToArray());
                var lamda = exp.Compile();
                var res = orderList.Where(lamda).ToList();
                totalRecord = res.Count;
                return res.Skip((pageNum - 1) * pageSize).Take(pageSize).ToList();
            }
            else
            {
                using (Model1 db = new Model1())
                {
                    try
                    {
                        var query = db.pre_order.AsQueryable();
                        List<pre_order> result = LinqHelperExtension.Where(query, condition.ToArray()).ToList();
                        totalRecord = result.Count;
                        orderList.AddRange(result);
                        return result.Skip((pageNum - 1) * pageSize).Take(pageSize).ToList();
                    }
                    catch (Exception ex)
                    {
                        Trace.WriteLine(DateTime.Now + ex.Message + "\n\r");
                        throw ex;
                    }
                }
            }

        }
    }
}
